using Photon;
using UnityEngine;

[RequireComponent(typeof(PhotonView))]
public class RCC_PhotonNetwork : Photon.MonoBehaviour
{
	private RCC_CarControllerV3 controllerScript;

	private Rigidbody rigid;

	private RCC_Camera cameraScript;

	private Vector3 correctPlayerPos;

	private Quaternion correctPlayerRot;

	private Vector3 currentVelocity;

	private float updateTime;

	private float gasInput;

	private float brakeInput;

	private float steerInput;

	private float handbrakeInput;

	private float boostInput;

	private float clutchInput;

	private float idleInput;

	private int gear;

	private int direction = 1;

	private bool changingGear;

	private bool semiAutomaticGear;

	private float fuelInput = 1f;

	private bool engineRunning;

	private bool lowBeamHeadLightsOn;

	private bool highBeamHeadLightsOn;

	private RCC_CarControllerV3.IndicatorsOn indicatorsOn;

	private void Start()
	{
		cameraScript = GetComponent<RCC_Camera>();
		controllerScript = GetComponent<RCC_CarControllerV3>();
		rigid = GetComponent<Rigidbody>();
		if (base.photonView.isMine)
		{
			if ((bool)cameraScript)
			{
				cameraScript.gameObject.SetActive(true);
			}
			controllerScript.canControl = true;
		}
		else
		{
			if ((bool)cameraScript)
			{
				cameraScript.gameObject.SetActive(false);
			}
			controllerScript.canControl = false;
		}
		base.gameObject.name = base.gameObject.name + base.photonView.viewID;
	}

	public void FixedUpdate()
	{
		if (!base.photonView.isMine)
		{
			controllerScript.AIController = true;
			Vector3 b = correctPlayerPos + currentVelocity * (Time.time - updateTime);
			if (Vector3.Distance(base.transform.position, correctPlayerPos) < 15f)
			{
				base.transform.position = Vector3.Lerp(base.transform.position, b, Time.deltaTime * 5f);
				base.transform.rotation = Quaternion.Lerp(base.transform.rotation, correctPlayerRot, Time.deltaTime * 5f);
			}
			else
			{
				base.transform.position = correctPlayerPos;
				base.transform.rotation = correctPlayerRot;
			}
			controllerScript.gasInput = gasInput;
			controllerScript.brakeInput = brakeInput;
			controllerScript.steerInput = steerInput;
			controllerScript.handbrakeInput = handbrakeInput;
			controllerScript.boostInput = boostInput;
			controllerScript.clutchInput = clutchInput;
			controllerScript.idleInput = idleInput;
			controllerScript.currentGear = gear;
			controllerScript.direction = direction;
			controllerScript.changingGear = changingGear;
			controllerScript.engineRunning = engineRunning;
			controllerScript.lowBeamHeadLightsOn = lowBeamHeadLightsOn;
			controllerScript.highBeamHeadLightsOn = highBeamHeadLightsOn;
			controllerScript.indicatorsOn = indicatorsOn;
		}
	}

	private void OnPhotonSerializeView(PhotonStream stream, PhotonMessageInfo info)
	{
		if (stream.isWriting)
		{
			stream.SendNext(controllerScript.gasInput);
			stream.SendNext(controllerScript.brakeInput);
			stream.SendNext(controllerScript.steerInput);
			stream.SendNext(controllerScript.handbrakeInput);
			stream.SendNext(controllerScript.boostInput);
			stream.SendNext(controllerScript.clutchInput);
			stream.SendNext(controllerScript.idleInput);
			stream.SendNext(controllerScript.currentGear);
			stream.SendNext(controllerScript.direction);
			stream.SendNext(controllerScript.changingGear);
			stream.SendNext(controllerScript.engineRunning);
			stream.SendNext(controllerScript.lowBeamHeadLightsOn);
			stream.SendNext(controllerScript.highBeamHeadLightsOn);
			stream.SendNext(controllerScript.indicatorsOn);
			stream.SendNext(base.transform.position);
			stream.SendNext(base.transform.rotation);
			stream.SendNext(rigid.velocity);
		}
		else
		{
			gasInput = (float)stream.ReceiveNext();
			brakeInput = (float)stream.ReceiveNext();
			steerInput = (float)stream.ReceiveNext();
			handbrakeInput = (float)stream.ReceiveNext();
			boostInput = (float)stream.ReceiveNext();
			clutchInput = (float)stream.ReceiveNext();
			idleInput = (float)stream.ReceiveNext();
			gear = (int)stream.ReceiveNext();
			direction = (int)stream.ReceiveNext();
			changingGear = (bool)stream.ReceiveNext();
			engineRunning = (bool)stream.ReceiveNext();
			lowBeamHeadLightsOn = (bool)stream.ReceiveNext();
			highBeamHeadLightsOn = (bool)stream.ReceiveNext();
			indicatorsOn = (RCC_CarControllerV3.IndicatorsOn)(int)stream.ReceiveNext();
			correctPlayerPos = (Vector3)stream.ReceiveNext();
			correctPlayerRot = (Quaternion)stream.ReceiveNext();
			currentVelocity = (Vector3)stream.ReceiveNext();
			updateTime = Time.time;
		}
	}
}
